<template>
  <div class="app-container">
  <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="商户名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入商户名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="商户状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="商户状态" clearable>
          <el-option
            v-for="status in statusArr"
            :key="status.value"
            :label="status.label"
            :value="status.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="allList" stripe style="width: 100%">
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="scenicName" label="所属景区"></el-table-column>
      <el-table-column prop="shopName" label="商户名称"></el-table-column>
      <el-table-column prop="percent" label="分成比例"></el-table-column>
      <el-table-column prop="username" label="经营者"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="count" label="二维码数量"></el-table-column>
      <el-table-column prop="qrcodeUrl" label="二维码">
        <template slot-scope="scope">
          <el-image :src="scope.row.qrcodeUrl" fit="cover" style="width:100px;height:100px" :preview-src-list="[scope.row.qrcodeUrl]"></el-image>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" >
        <template slot-scope="scope">
          <el-button @click="editRow(scope.row)" type="text" size="small">修改</el-button>
          <el-button @click="deleteRow(scope.row)" type="text" size="small">删除</el-button>
          <el-button @click="getQRCodeFn(scope.row)" type="text" size="small">生成小程序码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
    <el-dialog title="修改信息" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form"  label-width="80px">
        <el-row>
          <el-col >
            <el-form-item label="商户名称" prop="shopName">
              <el-input v-model="form.shopName" placeholder="请输入景点名称" />
            </el-form-item>
            <el-form-item label="经营者" prop="username">
              <el-input v-model="form.username" placeholder="请输入经营者" />
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" />
            </el-form-item>
            <el-form-item label="分成比例" prop="percent">
              <el-input v-model="form.percent" placeholder="请输入分成比例" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getShop, getById, updateShop, deleteShop, setStatus,getQRCode} from "@/api/system/shopManage";
export default {
  data() {
    return {
      loading: true,
      allList: [],
      total:0,
      queryParams: {
        current: 1,
        size: 10,
        status:'',
        shopName:''
      },
      statusArr:[{
        label:'营业中',
        value:1
      },{
        label:'休息中',
        value:2
      }],
      // 表单参数
      form: {},
      open: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //获取列表数据
    getList() {
      this.loading = true;
      getShop({
        ...this.queryParams,
      }).then(response => {
        this.allList = response.data?.records;
        this.total = response.data?.total;
        this.loading = false;
      });
    },
    reset() {
      this.form = {
        id: undefined,
      };
      this.resetForm("form");
    },
    editRow(row) {
      this.reset();
      this.form.id = row.id;
      this.open = true;
      getById(this.form).then(res => {
       this.form = res.data
      })
    },
    getQRCodeFn(row){
      getQRCode({shopId: row.id}).then(res => {
        this.$modal.msgSuccess("小程序码生成成功");
        this.getList();
      })
    },
    deleteRow(row){
      this.$confirm('此操作将永久删除该商户信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteShop({id: row.id}).then(res => {
          this.$modal.msgSuccess("商户信息删除成功");
          this.getList()
        })
      })
    },
    previewFn(url){

    },
    submitForm(){ 
      this.$refs["form"].validate(valid => {
        if (valid) {
          let data = {
            id: this.form.id,
            shopName: this.form.shopName,
            phone: this.form.phone,
            username: this.form.username,
            percent: this.form.percent,
            status: this.form.status,
          }
          updateShop(data).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    }
  },
};
</script>

<style>
</style>